<template>
  <div class="login-container">
    <el-form class="form-container" autoComplete="on" ref="form" :model="form" :rules="formRules">

      <h3>系统登录</h3>

      <el-form-item prop="username">
        <el-input v-model="form.username" placeholder="账户"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="form.password" placeholder="密码"></el-input>
      </el-form-item>

      <el-button type="primary" style="width:100%;margin-bottom:30px;"
                 @click.native.prevent="handleLogin">登录
      </el-button>
    </el-form>

  </div>
</template>
<script>
  export default {
    data () {
      const validateUsername = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码不能小于6位'))
        } else {
          callback()
        }
      }
      return {
        form: {
          username: 'admin',
          password: 'password'
        },
        formRules: {
          username: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePassword}]
        }
      }
    },
    methods: {
      handleLogin () {
        this.$refs.form.validate(valid => {
          console.log(valid)
          if (valid) {
            this.$router.push({path: '/'})
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style lang="less" rel="stylesheet/less">
  .login-container {
    .form-container {
      margin: 0 auto;
      margin-top: 80px;
      width: 400px;

      h3 {
        text-align: center;
      }
    }

  }
</style>
